/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.sunspotworld.executor;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.Writer;

/**
 * This class provides all the File related operations
 * @author Laili Aidi - aidi@kth.se
 * Condroid Summer CSD 2011 - KTH
 */
public class FileExecutor {

    private Writer obWriter = null;
    private File obFile = null;
    private String obStrFilePath;
    private CommandExecutor obCommandExecutor;

    /**
     * This is the constructor of FileExecutor class
     * @param obStrFilePath
     */
    public FileExecutor(String obStrFilePath) {
        this.obStrFilePath = obStrFilePath;
        this.obCommandExecutor = new CommandExecutor();
    }

    /**
     * This method is used to write the sensing data to a file
     * @param obStrFileName
     * @param obStrText
     * @return
     */
    public boolean writeFile(String obStrFileName, String obStrText) {
        try {
            obFile = new File(obStrFilePath + "/" + obStrFileName);
            boolean isSuccess = obFile.exists();
            //Check if this file is exist or not
            if (!isSuccess) {
                CommandExecutor obCmdExecute = new CommandExecutor();
                obCmdExecute.runCommand("touch " + obStrFilePath + "/" + obStrFileName);
                System.out.println("MESSAGE: file printed " + obStrFilePath + "/" + obStrFileName);
                this.obFile = new File(obStrFilePath + "/" + obStrFileName);
                isSuccess = this.obFile.exists();
            }

            if (isSuccess) {
                RandomAccessFile obRndAccessFile = null;
                obRndAccessFile = new RandomAccessFile(obStrFilePath + "/" + obStrFileName, "rw");
                obRndAccessFile.seek(obRndAccessFile.length());
                obRndAccessFile.writeBytes(obStrText + "\n");
                obRndAccessFile.close();

            }
        } catch (FileNotFoundException ex) {
            System.err.println("ERROR Caught writeFile 1: " + ex.getMessage() + " while writing sensor samples to the file.");
            return false;
        } catch (IOException e) {
            System.err.println("ERROR writeFile 2: " + e.getMessage() + " while writing sensor samples to the file.");
            return false;
        }
        return true;
    }

    /**
     * 
     * This method is used to read the sensing data from a file
     * @param obStrFileName
     * @return
     */
    public String readFile(String obStrFileName) {
        String obStrText = "";
        try {
            RandomAccessFile raf = new RandomAccessFile(obStrFilePath + "/" + obStrFileName, "r");
            String line = null;
            while ((line = raf.readLine()) != null) {
                obStrText += line + "\n";
            }
            if (obStrText.length() > 0) {
                return obStrText;
            }
        } catch (FileNotFoundException ex) {
            System.err.println("ERROR Caught readFile 1: " + ex.getMessage() + " while reading sensor samples from the file.");
            return null;
        } catch (IOException e) {
            System.err.println("ERROR Caught readFile 2: " + e.getMessage() + " while reading sensor samples from the file.");
            return null;
        }
        return null;
    }

    /**
     * 
     * This method is used to compress the sensing data file. 
     * The tar command is used to create an archive (not compressed), 
     * and another (gzip or compress) is used to compress the archive.
     * -zcvf /tmp/compress_here/archive_name.tar.gz file_name
     * @param obStrFileName
     * @return isSuccess
     */
    public boolean compresFile(String obStrFileName) {
        File file = new File(obStrFilePath + "/" + obStrFileName);
        boolean exists = file.exists();
        // System.out.println("compresFile File " + (obStrFilePath + "/" + obStrFileName) + " =" + exists);
        if (!exists) {
            // It returns false if File or directory does not exist
            //System.out.println("MESSAGE: File " + oobStrFilePath + "/" + obStrFileName +" is not exist");
        } else {
            boolean isSuccess = obCommandExecutor.runCommand("mv " + obStrFilePath + "/" + obStrFileName + " " + obStrFileName);
            System.out.println("mv " + obStrFilePath + "/" + obStrFileName + " " + obStrFileName);
            if (isSuccess) {
                isSuccess = obCommandExecutor.runCommand("tar -cvzf " + obStrFilePath + "/" + obStrFileName + ".tar.gz " + obStrFileName);
                System.out.println("tar -cvzf " + obStrFilePath + "/" + obStrFileName + ".tar.gz " + obStrFileName);
                if (isSuccess) {
                    System.out.println("MESSAGE: Compresing file was successful");
                    File fileOriginal = new File(obStrFileName);
                    exists = fileOriginal.exists();
                    //Even if can't send it as bundle now, keep delete it, we try in another round.
                    if (exists || fileOriginal.canWrite()) {
                        fileOriginal.delete();
                    }
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 
     * This method is used to uncompress the sensing data file. 
     * The tar command is used to create an archive (not compressed).
     * tar -zxvf archive_name.tar.gz /tmp/extract_here/file_name
     * @param obStrReceiverName
     * @param obStrFileName
     * @return isSuccess
     */
    public boolean uncompressFile(String obStrReceiverName, String obStrFileName) {
        File file = new File(obStrFilePath + "/" + obStrReceiverName + "/" + obStrFileName + ".tar.gz");
        boolean exists = file.exists();
        if (!exists) {
            // It returns false if File or directory does not exist
            //System.out.println("MESSAGE: File " + obStrFilePath + "/" + obStrFileName + ".tar.gz is not exist");
        } else {
            boolean isSuccess = obCommandExecutor.runCommand("tar -zxvf " + obStrFilePath + "/" + obStrReceiverName + "/" + obStrFileName + ".tar.gz");
            System.out.println("tar -zxvf " + obStrFilePath + "/" + obStrReceiverName + "/" + obStrFileName + ".tar.gz");
            if (isSuccess) {
                isSuccess = obCommandExecutor.runCommand("mv " + obStrFileName + " " + obStrFilePath);
                System.out.println("mv " + obStrFileName + " " + obStrFilePath);
                if (isSuccess) {
                    System.out.println("MESSAGE: Uncompresing file was successful");
                    return true;
                }
            }
        }
        return false;
    }
}
